/*=============================================================================

  Library: HMPBundleFrame

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include "Version.h"

#include "Exception.h"

#include <QStringListIterator>
#include <QDebug>


const QString Version::SEPARATOR = ".";
const QRegExp Version::RegExp = QRegExp("[a-zA-Z0-9_\\-]*");

//----------------------------------------------------------------------------
Version Version::emptyVersion()
{
  static Version emptyV(false);
  return emptyV;
}

//----------------------------------------------------------------------------
Version Version::undefinedVersion()
{
  static Version undefinedV(true);
  return undefinedV;
}

//----------------------------------------------------------------------------
Version& Version::operator=(const Version& v)
{
  majorVersion = v.majorVersion;
  minorVersion = v.minorVersion;
  microVersion = v.microVersion;
  qualifier = v.qualifier;
  undefined = v.undefined;
  return *this;
}

//----------------------------------------------------------------------------
Version::Version(bool undefined)
  : majorVersion(0), minorVersion(0), microVersion(0), qualifier(""), undefined(undefined)
{

}

//----------------------------------------------------------------------------
void Version::validate()
{
  if (!RegExp.exactMatch(qualifier))
    throw InvalidArgumentException(QString("invalid qualifier: ") + qualifier);

  undefined = false;
}

//----------------------------------------------------------------------------
Version::Version(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion)
  : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(""),
    undefined(false)
{

}

//----------------------------------------------------------------------------
Version::Version(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const QString& qualifier)
   : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(qualifier),
     undefined(true)
{
  this->validate();
}

//----------------------------------------------------------------------------
Version::Version(const QString& version)
  : majorVersion(0), minorVersion(0), microVersion(0), undefined(true)
{
  unsigned int maj = 0;
  unsigned int min = 0;
  unsigned int mic = 0;
  QString qual("");

  QStringList st = version.split(SEPARATOR);

  if (st.empty()) return;

  QStringListIterator i(st);

  bool ok = true;
  maj = i.next().toUInt(&ok);

  if (i.hasNext())
  {
    min = i.next().toUInt(&ok);
    if (i.hasNext())
    {
      mic = i.next().toUInt(&ok);
      if (i.hasNext())
      {
        qual = i.next();
        if (i.hasNext())
        {
           ok = false;
        }
      }
    }
  }

  if (!ok) throw InvalidArgumentException("invalid format");

  majorVersion = maj;
  minorVersion = min;
  microVersion = mic;
  qualifier = qual;
  this->validate();
}

//----------------------------------------------------------------------------
Version::Version(const Version& version)
: majorVersion(version.majorVersion), minorVersion(version.minorVersion),
  microVersion(version.microVersion), qualifier(version.qualifier),
  undefined(version.undefined)
{

}

//----------------------------------------------------------------------------
Version Version::parseVersion(const QString& version)
{
  if (version.isEmpty())
  {
    return emptyVersion();
  }

  QString version2 = version.trimmed();
  if (version2.isEmpty())
  {
    return emptyVersion();
  }

  return Version(version2);
}

//----------------------------------------------------------------------------
bool Version::isUndefined() const
{
  return undefined;
}

//----------------------------------------------------------------------------
unsigned int Version::getMajor() const
{
  if (undefined) throw IllegalStateException("Version undefined");
  return majorVersion;
}

//----------------------------------------------------------------------------
unsigned int Version::getMinor() const
{
  if (undefined) throw IllegalStateException("Version undefined");
  return minorVersion;
}

//----------------------------------------------------------------------------
unsigned int Version::getMicro() const
{
  if (undefined) throw IllegalStateException("Version undefined");
  return microVersion;
}

//----------------------------------------------------------------------------
QString Version::getQualifier() const
{
  if (undefined) throw IllegalStateException("Version undefined");
  return qualifier;
}

//----------------------------------------------------------------------------
QString Version::toString() const
{
  if (undefined) return "undefined";

  QString result;
  result += QString::number(majorVersion) + SEPARATOR + QString::number(minorVersion) + SEPARATOR + QString::number(microVersion);
  if (!qualifier.isEmpty())
  {
    result += SEPARATOR + qualifier;
  }
  return result;
}

//----------------------------------------------------------------------------
bool Version::operator==(const Version& other) const
{
  if (&other == this)
  { // quicktest
    return true;
  }

  if (other.undefined && this->undefined) return true;
  if (this->undefined) throw IllegalStateException("Version undefined");
  if (other.undefined) return false;

  return (majorVersion == other.majorVersion) && (minorVersion == other.minorVersion) && (microVersion
      == other.microVersion) && qualifier == other.qualifier;
}

//----------------------------------------------------------------------------
int Version::compare(const Version& other) const
{
  if (&other == this)
  { // quicktest
    return 0;
  }

  if (this->undefined || other.undefined)
    throw IllegalStateException("Cannot compare undefined version");

  if (majorVersion < other.majorVersion)
  {
    return -1;
  }

  if (majorVersion == other.majorVersion)
  {

    if (minorVersion < other.minorVersion)
    {
      return -1;
    }

    if (minorVersion == other.minorVersion)
    {

      if (microVersion < other.microVersion)
      {
        return -1;
      }

      if (microVersion == other.microVersion)
      {
        return qualifier.compare(other.qualifier);
      }
    }
  }
  return 1;
}

//----------------------------------------------------------------------------
bool Version::operator <(const Version &object) const
{
  return this->compare(object) < 0;
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, const Version& v)
{
  dbg << v.toString();

  return dbg.maybeSpace();
}
